<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class User extends Backend
{

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $list = collection($list)->toArray();
            $ids = array_column($list, 'id');
            if ($ids) {
                $userRealName = $this->model
                    ->name('user_real_name')
                    ->where('user_id', 'in', array_unique($ids))
                    ->where('status', 3)
                    ->field('MAX(id) id')
                    ->group('user_id')
                    ->select();
                $userRealName = collection($userRealName)->toArray();
                if ($userRealName) {
                    $ids = array_column($userRealName, 'id');
                    if ($ids) {
                        $userRealName = $this->model
                            ->name('user_real_name')
                            ->where('id', 'in', array_unique($ids))
                            ->column('auth_table,handheld_card', 'user_id');
                        $userRealName = collection($userRealName)->toArray();
                        foreach ($list as &$v) {
                            $v['user_real_name'] = isset($userRealName[$v['id']]) ? $userRealName[$v['id']] : [];
                        }
                    }
                }
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function realname()
    {
        if ($this->request->isAjax()) {
            $filter = json_decode($this->request->get("filter", ''), true);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->name('user_real_name')
                ->where($where)
                ->where('status', 1)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->name('user_real_name')
                ->where($where)
                ->where('status', 1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $list = collection($list)->toArray();
            $ids = array_column($list, 'user_id');
            if ($ids) {
                $users = $this->model
                    ->name('user')
                    ->where('id', 'in', array_unique($ids))
                    ->field('id,number,mobile')
                    ->select();
                $users = collection($users)->toArray();
                $user = [];
                foreach ($users as $v) {
                    $id = $v['id'];
                    unset($v['id']);
                    $user[$id] = $v;
                }
                foreach ($list as &$v) {
                    $v = array_merge($v, isset($user[$v['user_id']]) ? $user[$v['user_id']] : []);
                }
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function confirm($ids = "")
    {
        if ($ids) {
            $realName = $this->model->name('user_real_name')->where('id', 'in', $ids)->find();
            $this->model->transaction(function () use($ids, $realName) {
                $this->model->name('user')->where('id', $realName['user_id'])->update(['status' => 3]);
                $this->model->name('user_real_name')->where('id', 'in', $ids)->update(['status' => 3]);
            });
            $this->success(__('success'));
        }
        $this->error(__('error'));
    }

    public function unqualified($ids = "")
    {
        if ($ids) {
            $realName = $this->model->name('user_real_name')->where('id', 'in', $ids)->find();
            $this->model->transaction(function () use($ids, $realName) {
                $this->model->name('user')->where('id', $realName['user_id'])->update(['status' => 2]);
                $this->model->name('user_real_name')->where('id', 'in', $ids)->update(['status' => 2]);
            });
            $this->success(__('Success'));
        }
        $this->error(__('Error'));
    }

}
